#include "widget.h"
#include "player1.h"
#include "player2.h"
#include "ui_widget.h"
#include <Qpixmap>
#include<QPushButton>
#include<QGraphicsTextItem>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , m_Scene1(this)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(1270, 819);
    Startbtn = new QPushButton;
    m_Scene1.setSceneRect(QRect(0, 0, 1270, 819));
    QPixmap bgPixmap1(":/lng/picture/BackGround1.png");
    bgPixmap1 = bgPixmap1.scaled(1270, 819); // 确保图片严格匹配场景尺寸
    m_BackGround1.setPixmap(bgPixmap1);
    m_Scene1.addItem(&m_BackGround1);

    // 关键设置
    m_View.setScene(&m_Scene1);
    m_View.setParent(this);
    m_View.setFixedSize(1271, 823);

    m_View.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_View.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    // 禁用视图变换（可选双重保障）
    m_View.setTransformationAnchor(QGraphicsView::NoAnchor);
    m_View.setResizeAnchor(QGraphicsView::NoAnchor);

    m_View.show();

    //开始按钮
    Startbtn->setParent(this);
    Startbtn->setFixedSize(800,65);
    //QFont font1("楷体",40,10,1);   //字体
    //Startbtn->setFont(font1);
   /* Startbtn->setStyleSheet("QPushButton{background-color:rgba(135,219,93,1); }\
                            QPushButton{background-image: url(:/images/btn_normal.png);}\
                            QPushButton:hovor{background-color:rgba(135,219,93,1); }\
                            QPushButton:pressed{background-color:rgba(135,219,93,1); }");*/
    Startbtn->setStyleSheet(
        "QPushButton {"
        "   border: none;"                        // 去掉边框
        "   background-image: url(:/lng/picture/Startbtn.png);" // 正常状态图片
        "   background-repeat: no-repeat;"        // 禁止重复平铺
        "   background-position: center;"         // 图片居中
        "}"
        "QPushButton:hover {"
        "   background-image: url(:/lng/picture/StartbtnHover.png);"  // 悬停状态图片
        "}"
        );
    Startbtn->move(240,610);
    Startbtn->show();

    connect(Startbtn,&QPushButton::clicked,this,&Widget::InitGame);//点击按钮后切换场景
}
void Widget::InitGame()
{
    //切换游戏场景
    QPixmap bgPixmap1(":/lng/picture/GameBackGround.png");
    bgPixmap1 = bgPixmap1.scaled(1270, 819); // 确保图片严格匹配场景尺寸
    m_Scene2.setSceneRect(QRect(0, 0, 1270, 819));
    m_BackGround2.setPixmap(bgPixmap1);
    m_Scene2.addItem(&m_BackGround2);
    m_View.setScene(&m_Scene2);

    //删除初始按钮
    delete Startbtn;
    Startbtn = nullptr;

    //计数器
    m_counterText = new QGraphicsTextItem;
    m_counterText->setPlainText("Score 0 : 0");
    m_counterText->setDefaultTextColor(Qt::white); // 文字颜色
    m_counterText->setFont(QFont("Arial", 20));     // 字体和大小
    m_counterText->setPos(20, 20);                 // 位置（左上角）
    m_counterText->setZValue(1);                // 确保在最上层
    m_Scene2.addItem(m_counterText);

    //建立羽毛球
    QPixmap badmintonPixmap(":/lng/picture/OIP-C.jpg");
    badminton = new Badminton(badmintonPixmap);
    badminton->setPos(600, 300);
    m_Scene2.addItem(badminton);

    // 示例：模拟击打羽毛球
    badminton->hit(5, -3);

    //设置Player1
    Player1 *player1 = new Player1();
    player1->setPos(1000,600);
    m_Scene2.addItem(player1);
    m_Scene2.addItem(player1->m_Racket);
    // 假设球拍把手固定在火柴人图片底部中心位置 少了这步一开始球拍不在火柴人身上
    QPointF FixedPos = player1->pos() + QPointF(player1->boundingRect().width() / 2 - 3, player1->boundingRect().height()/2 - 20);
    player1->m_Racket->SetStickPosition(FixedPos);

    Player2 *player2 = new Player2();
    player2->setPos(400,600);
    m_Scene2.addItem(player2);
    m_Scene2.addItem(player2->m_Racket);
    // 假设球拍把手固定在火柴人图片底部中心位置 少了这步一开始球拍不在火柴人身上
    QPointF FixedPos1 = player2->pos() + QPointF(player2->boundingRect().width() / 2 - 3, player2->boundingRect().height()/2 - 20);
    player2->m_Racket->SetStickPosition(FixedPos1);

    //绘制空气墙
    // 创建空气墙，这里以场景底部为例
    QGraphicsRectItem *AirWall1 = new QGraphicsRectItem(0 , 0 , 100 , 1000); //左空气墙
    QGraphicsRectItem *AirWall2 = new QGraphicsRectItem(607 , 600 , 40 , 1000); //球网空气墙
    QGraphicsRectItem *AirWall3 = new QGraphicsRectItem(1170 , 0 , 100 , 1000); //右空气墙
    AirWall1->setPen(Qt::NoPen); // 不绘制边框
    m_Scene2.addItem(AirWall1);
    AirWall2->setPen(Qt::NoPen); // 不绘制边框
    m_Scene2.addItem(AirWall2);
    AirWall3->setPen(Qt::NoPen); // 不绘制边框
    m_Scene2.addItem(AirWall3);
}

//比分增长
void Widget::IncreaseScore(int value1,int value2) {
    static int score = 0;
    score += value1;
    m_counterText->setPlainText(QString("Score ").arg(score));
}
Widget::~Widget()
{
    delete ui;
}
